package com.jd.org.controller;



import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Dictionarytype;
import com.jd.org.query.DictionarytypeQuery;
import com.jd.org.service.IDictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：字典
 */
@RestController
@RequestMapping("/dictionarytype")
@Api(value = "字典接口",description = "字典接口")
public class DictionaytypeController {

    @Autowired
    private IDictionarytypeService dictionarytypeService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //http://localhost:8080/dictionarytype/1 + get
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")
    public Dictionarytype loadOne(@PathVariable("id")Long id){
        return dictionarytypeService.loadById(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    //http://localhost:8080/dictionarytype + 无 + get
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    public List<Dictionarytype> loadAll(){
        return dictionarytypeService.loadAll();
    }

    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    //http://localhost:8080/dictionarytype + {json对象} + post
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Dictionarytype> queryPage(@RequestBody DictionarytypeQuery query){
        return dictionarytypeService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    //http://localhost:8080/dictionarytype/1 + removeete
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dictionarytypeService.remove(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param dictionarytype
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/dictionarytype + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionarytype dictionarytype){
        try {
            if(dictionarytype.getId() == null){//添加
                dictionarytypeService.save(dictionarytype);
            }else{
                dictionarytypeService.update(dictionarytype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }



    /**
     * 接口：批量删除
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            dictionarytypeService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

}